﻿using Reader.Libs.BOOKOBJ;
using Reader.UC.Dialogs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;

namespace Reader.UC
{
    /// <summary>
    /// Interaction logic for LibraryUC.xaml
    /// </summary>
    public partial class LibraryUC : UserControl
    {
        public BookLib booklib;
        public event EventHandler OnOpenFly;
        public event EventHandler OnReadBook;

        public LibraryUC()
        {
            booklib = new BookLib();
            InitializeComponent();
            this.Loaded += this.OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(BookLib));
                if (File.Exists("BookLib.xml"))
                    using (FileStream fileStream = new FileStream("BookLib.xml", FileMode.Open))
                    {
                        booklib = (BookLib)serializer.Deserialize(fileStream);
                        (this.DataContext as MainWindowDataContext).BookList = new ObservableCollection<Book>(booklib.BookList);
                        fileStream.Close();
                    }
            }
            catch { throw; } 
        }

        private void SaveBookLib()
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(BookLib));
                using (StreamWriter streamWriter = new StreamWriter("BookLib.xml"))
                {
                    serializer.Serialize(streamWriter, booklib);
                    streamWriter.Close();
                }
            }
            catch { throw; } 
        }

        private void BtnFlyout_Click(object sender, RoutedEventArgs e)
        {
            if (this.OnOpenFly != null)
                this.OnOpenFly(this, EventArgs.Empty);
        }

        private void AddBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                AddBookDialog ad = new AddBookDialog(this);
                ad.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error! +\n" + ex.Message);
            }
        }

        private void EditBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.LibListBox.SelectedItems.Count == 0 )
                {
                    this.LibListBox.SelectedIndex = 0;
                }
                if (this.LibListBox.SelectedItems.Count > 0)
                {
                    Book book = (from b in booklib.BookList
                                 where b == (this.LibListBox.SelectedItems[0] as Book)
                                 select b).First();

                    EditBookDialog ad = new EditBookDialog(this, book);
                    ad.ShowDialog();
                } 
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error! \n" + ex.Message);
            }
        }

        private void DeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.LibListBox.SelectedItems.Count > 0)
                {
                    Book book = (from b in booklib.BookList
                                 where b == (this.LibListBox.SelectedItems[0] as Book)
                                 select b).First();

                    DeleteConfirmDialog dcd = new DeleteConfirmDialog();
                    dcd.OnDeleteAccept += DeleteBook;
                    dcd.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error! \n" + ex.Message);
            }
        }

        private void ReadBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.OnReadBook != null)
                    this.OnReadBook(this, EventArgs.Empty);            
        }

        private void ConvertBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ConvertDialog dg;
                if (this.LibListBox.SelectedItems.Count > 0)
                {
                    Book book = (from b in booklib.BookList
                                 where b == (this.LibListBox.SelectedItems[0] as Book)
                                 select b).First();
                    dg = new ConvertDialog(book);
                }
                else
                {
                    dg = new ConvertDialog(null);
                }
                dg.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error! \n" + ex.Message);
            }
        }

        public void AddBook(Book b)
        {
            try
            {
                booklib.BookList.Add(b);
                (this.DataContext as MainWindowDataContext).BookList = new ObservableCollection<Book>(booklib.BookList);
                SaveBookLib();
            }
            catch
            {
                throw;
            }
        }

        public void EditBook(Book b)
        {
            try
            {
                (this.DataContext as MainWindowDataContext).BookList = new ObservableCollection<Book>(booklib.BookList);
                SaveBookLib();
            }
            catch
            {
                throw;
            }
        }

        public void DeleteBook(object sender, EventArgs e)
        {
            try
            {
                if (this.LibListBox.SelectedItems.Count > 0)
                {
                    Book book = (from b in booklib.BookList
                                 where b == (this.LibListBox.SelectedItems[0] as Book)
                                 select b).First();

                    booklib.BookList.Remove(book);
                    (this.DataContext as MainWindowDataContext).BookList = new ObservableCollection<Book>(booklib.BookList);
                    SaveBookLib();
                }
            }
            catch
            {
                throw;
            }
        }

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount >= 2)
                if (this.OnReadBook != null)
                    this.OnReadBook(this, EventArgs.Empty);
        }
    }
}
